package com.ys.otms.services.impl;

import com.ys.otms.dao.IBusinessAnalysisDao;
import com.ys.otms.dto.BusinessAnalysisDto;
import com.ys.otms.services.IBusinessAnalysisServices;
import com.ys.otms.utils.JsonUtil;
import com.ys.otms.utils.StringUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class BusinessAnalysisImpl implements IBusinessAnalysisServices {

    private static Logger logger = LoggerFactory.getLogger(BusinessAnalysisImpl.class);
    @Autowired
    private IBusinessAnalysisDao iBusinessAnalysisDao;

    @Override
    public JSONObject queryZYInfo(HttpServletRequest request) {
        logger.info("BusinessAnalysisImpl queryZYInfo start");
//        int companyId = 4;
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        int zYCount = iBusinessAnalysisDao.selectZYCount(companyId);
        int zYTransportShipCount = iBusinessAnalysisDao.selectZYTransportShipCount(companyId);
        int xZYShipCount = iBusinessAnalysisDao.selectXZYShipCount(companyId);
        JSONObject retObj = new JSONObject();
        retObj.put("zYCount", zYCount);
        retObj.put("zYTransportShipCount", zYTransportShipCount);
        retObj.put("xZYShipCount", xZYShipCount);
        retObj.put("SUCCESS", true);
        logger.info("BusinessAnalysisImpl queryZYInfo end");
        return retObj;
    }

    @Override
    public JSONObject querySZYLRank(HttpServletRequest request) {
        logger.info("BusinessAnalysisImpl querySZYLRank start");
//        int companyId = 4;
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        List<BusinessAnalysisDto> list = iBusinessAnalysisDao.selectSZYLRank(companyId);
        for (BusinessAnalysisDto businessAnalysisDto : list) {
            if (null == businessAnalysisDto.getSZTotal()) {
                businessAnalysisDto.setSZTotal(0d);
            }
        }
        JSONObject retObj = new JSONObject();
        retObj.put("data", list);
        retObj.put("SUCCESS", true);
        logger.info("BusinessAnalysisImpl querySZYLRank end");
        return retObj;
    }

    @Override
    public JSONObject queryHSLRank(HttpServletRequest request) {
        logger.info("BusinessAnalysisImpl queryHSLRank start");
//        int companyId = 4;
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        List<BusinessAnalysisDto> list = iBusinessAnalysisDao.selectHSLRank(companyId);
        for (BusinessAnalysisDto businessAnalysisDto : list) {
            if (null == businessAnalysisDto.getHsl()) {
                businessAnalysisDto.setHsl(0d);
            }
        }
        JSONObject retObj = new JSONObject();
        retObj.put("data", list);
        retObj.put("SUCCESS", true);
        logger.info("BusinessAnalysisImpl queryHSLRank end");
        return retObj;
    }

    @Override
    public JSONObject queryFHLRank(HttpServletRequest request) throws Exception {
        logger.info("BusinessAnalysisImpl queryFHLRank start");
        JSONObject obj = JsonUtil.getJSONObject(request);
        String beginTime = JsonUtil.getJSONObjectString(obj, "beginTime");
        String endTime = JsonUtil.getJSONObjectString(obj, "endTime");
        JSONObject retObj = new JSONObject();
        if (StringUtil.isEmpty(beginTime) || StringUtil.isEmpty(endTime)) {
            retObj.put("SUCCESS",false);
            retObj.put("MSG","查询时间为空！");
            return retObj;
        }
//        int companyId = 4;
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        BusinessAnalysisDto businessAnalysisDto = new BusinessAnalysisDto(beginTime,endTime,companyId);
        List<BusinessAnalysisDto> list = iBusinessAnalysisDao.selectFHLRate(businessAnalysisDto);
        double fhlTotal = 0d;
        for (BusinessAnalysisDto businessAnalysisDto1 : list) {
            fhlTotal += businessAnalysisDto1.getFhl();
        }
        for (BusinessAnalysisDto businessAnalysisDto1 : list) {
            businessAnalysisDto1.setFhlZB(businessAnalysisDto1.getFhl() * 100/fhlTotal);
        }
        retObj.put("fhlTotal", fhlTotal);
        retObj.put("data", list);
        retObj.put("SUCCESS", true);
        logger.info("BusinessAnalysisImpl queryFHLRank end");
        return retObj;
    }
}
